#!/bin/bash
rpms=(
    python-ply-3.4-11.el7.noarch.rpm
    python-pycparser-2.14-1.el7.noarch.rpm
    python-cffi-1.6.0-5.el7.x86_64.rpm
    python-enum34-1.0.4-1.el7.noarch.rpm
    python-idna-2.4-1.el7.noarch.rpm
    python-ipaddress-1.0.16-2.el7.noarch.rpm
    python2-pyasn1-0.1.9-7.el7.noarch.rpm
    python-backports-1.0-8.el7.x86_64.rpm
    python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
    python-setuptools-0.9.8-7.el7.noarch.rpm
    python-markupsafe-0.11-10.el7.x86_64.rpm
    python-babel-0.9.6-8.el7.noarch.rpm
    python2-cryptography-1.7.2-2.el7.x86_64.rpm
    python-paramiko-2.1.1-9.el7.noarch.rpm
    python2-httplib2-0.18.1-3.el7.noarch.rpm
    python-jinja2-2.7.2-4.el7.noarch.rpm
    python2-jmespath-0.9.4-2.el7.noarch.rpm
    sshpass-1.06-2.el7.x86_64.rpm
    ansible-2.9.27-1.el7.noarch.rpm
)

script_path=$(cd "$(dirname "$0")" && pwd -P)

function install_ansible() {
    echo "Installing ansible..."
    for rpm in "${rpms[@]}"; do
        # 提取 RPM 包名（去掉版本和扩展名）
        package_name=$(rpm -qp --qf '%{NAME}' "${script_path}/package/rpms/${rpm}")
        # 检查包是否已经安装
        if rpm -q "$package_name" > /dev/null 2>&1; then
            echo "$package_name is already installed, skipping..."
        else
            rpm -ivh "${script_path}/package/rpms/${rpm}"
            if [ $? -ne 0 ]; then
                echo "Error installing $rpm"
                exit 1
            fi
            echo "Installed $rpm"
        fi
    done
    echo "Ansible installed successfully"
}

function uninstall_ansible() {
    echo "Uninstalling ansible and its dependencies..."
    # 反向遍历 RPM 包数组，确保先卸载依赖较少的包
    for (( idx=${#rpms[@]}-1 ; idx>=0 ; idx-- )); do
        rpm="${rpms[$idx]}"
        # 提取 RPM 包名（去掉版本和扩展名）
        package_name=$(rpm -qp --qf '%{NAME}' "${script_path}/package/rpms/${rpm}")
        # 检查包是否已经安装
        if rpm -q "$package_name" > /dev/null 2>&1; then
            rpm -e "$package_name"
            if [ $? -ne 0 ]; then
                echo "Error uninstalling $package_name"
                exit 1
            fi
            echo "Uninstalled $package_name"
        else
            echo "$package_name is not installed, skipping..."
        fi
    done
    echo "Ansible and its dependencies uninstalled successfully"
}

function change_ansible_config() {
    # Change ansible config
    cat <<EOF >/etc/ansible/ansible.cfg
[defaults]
strategy_plugins = ${script_path}/package/mitogen-0.2.10-rc.0/ansible_mitogen/plugins/strategy
strategy = mitogen_linear
host_key_checking = False
EOF
    echo "Ansible config changed successfully"
}
# 将自动检测内核版本，如果小于4.19，则更新内核版本
function update_kernel() {
    echo "Updating kernel"
    ansible-playbook ${script_path}/update-kernel.yaml
}

function deploy_k8s() {
    echo "Deploying k8s"
    ansible-playbook ${script_path}/k8s-deploy.yaml
}

function main() {
    case $1 in
        "install_ansible")
        install_ansible
        change_ansible_config
        ;;
        "update_kernel")
        update_kernel
        ;;
        "deploy_k8s")
        deploy_k8s
        ;;
        "uninstall_ansible")
        uninstall_ansible
        ;;
        *)
        echo "Usage: $0 {install_ansible|uninstall_ansible|update_kernel|deploy_k8s}"
        ;;
    esac
}
main $1
